package com.xxxx.crm.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xxxx.crm.base.BaseController;
import com.xxxx.crm.base.ResultInfo;
import com.xxxx.crm.service.ClockService;
import com.xxxx.crm.vo.Clock;
import com.xxxx.crm.vo.User;

import java.util.Date;


@Controller
@RequestMapping("/clock")
public class ClockController extends BaseController {

    @Resource
    private ClockService clockService;

    /**
     * 跳转到打卡页面
     */
    @GetMapping("/welcome")
    public String welcome(HttpSession session, Model model) {
        // 从 session 中获取用户信息
        User user = (User) session.getAttribute("user");
        if (user != null) {
            model.addAttribute("user", user);
            // 获取当天打卡记录
            Clock todayClock = clockService.queryTodayClock(user.getId(), new Date());
            model.addAttribute("todayClock", todayClock);
        }
        return "welcome";
    }

    /**
     * 执行打卡操作
     */
    @PostMapping("/punch")
    @ResponseBody
    public ResultInfo punch(String userName, String clockType, String clockTime) {
        ResultInfo resultInfo = new ResultInfo();
        try {

            // 根据用户名查询用户ID并执行打卡
            if ("in".equals(clockType)) {

                Integer status = clockService.clockInWithUsername(userName, clockTime);
                if(status == 1){
                    resultInfo.setMsg("上班打卡成功！");
                }else {
                    resultInfo.setMsg("上班打卡失败！");
                }

            } else if ("out".equals(clockType)) {
                Integer status = clockService.clockOutWithUsername(userName, clockTime);
                if(status == 1){
                    resultInfo.setMsg("下班打卡成功！");
                }else {
                    resultInfo.setMsg("下班打卡失败！");
                }
            } else {
                resultInfo.setCode(300);
                resultInfo.setMsg("无效的打卡类型！");
                return resultInfo;
            }

            resultInfo.setCode(200);

        } catch (Exception e) {
            resultInfo.setCode(500);
            resultInfo.setMsg(e.getMessage());
        }
        return resultInfo;
    }

    /**
     * 获取当天打卡记录
     */
    @GetMapping("/today")
    @ResponseBody
    public ResultInfo getTodayClock(HttpSession session) {
        ResultInfo resultInfo = new ResultInfo();
        try {
            // 获取当前登录用户
            User user = (User) session.getAttribute("user");
            if (user == null) {
                resultInfo.setCode(300);
                resultInfo.setMsg("用户未登录！");
                return resultInfo;
            }

            // 查询打卡记录
            Clock todayClock = clockService.queryTodayClock(user.getId(), new Date());
            resultInfo.setResult(todayClock);
            resultInfo.setCode(200);

        } catch (Exception e) {
            resultInfo.setCode(500);
            resultInfo.setMsg(e.getMessage());
        }
        return resultInfo;
    }

    /**
     * 获取打卡记录列表
     */
    @GetMapping("/records")
    @ResponseBody
    public ResultInfo getClockRecords(HttpSession session, 
            @RequestParam(required = false) String dateRange) {
        ResultInfo resultInfo = new ResultInfo();
        try {
            User user = (User) session.getAttribute("user");
            if (user == null) {
                resultInfo.setCode(300);
                resultInfo.setMsg("用户未登录！");
                return resultInfo;
            }
            
            // TODO: 实现打卡记录查询逻辑
            // 这里需要在 ClockService 中添加相应的查询方法
            
            resultInfo.setCode(200);
        } catch (Exception e) {
            resultInfo.setCode(500);
            resultInfo.setMsg(e.getMessage());
        }
        return resultInfo;
    }
}
